//
//  PayIntentionViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/23.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "PayIntentionViewController.h"
#import "House.h"
#import "HttpServer.h"
#import "PersonAccountViewController.h"
#import <TSMessages/TSMessageView.h>
#import <MobClick.h>
@interface PayIntentionViewController ()
{
    int balance;
    int totalEarnests;
    int trustee;
}

@end

@implementation PayIntentionViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES];
    [MobClick beginLogPageView:@"支付设置页面"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
     [MobClick endLogPageView:@"支付设置页面"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.payButtonView setTitle:@"确认支付定金250元" forState:UIControlStateNormal];
    _selectImage_1.hidden = NO;
    _selectImage_2.hidden = YES;
    _selectImage_3.hidden = YES;
    NSLog(@"%lu",(unsigned long)_scheduleInfo.scheduleArray.count);
    NSLog(@"%@",[NSString stringWithFormat:@"%@",_scheduleInfo.scheduleArray]);
    for (int i = 0; i < _scheduleInfo.scheduleArray.count; i++)
    {
        House*house = _scheduleInfo.scheduleArray[i];
        NSLog(@"%@",house.houseCommunity);
        if (i == 0)
        {
            if ([house.houseCommunity isEqualToString:@""] || house.houseCommunity==NULL)
            {
                _houseLabel_1.text = @"";
            }
            else
            {
                _houseLabel_1.text = house.houseCommunity;
            }
            
        }
        if (i == 1)
        {
            if ([house.houseCommunity isEqualToString:@""] || house.houseCommunity==NULL)
            {
                _houseLabel_2.text = @"";
            }
            else
            {
                _houseLabel_2.text = house.houseCommunity;
            }
        }
        if (i == 2)
        {
            if ([house.houseCommunity isEqualToString:@""] || house.houseCommunity==NULL)
            {
                _houseLabel_3.text = @"";
            }
            else
            {
                _houseLabel_3.text = house.houseCommunity;
            }
        }
    }
    
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
-(void)loadData
{
    
    [HttpServer getEarnestsWithScheduleId:_scheduleId WithSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"%@",responseData);
        balance = [responseData[@"balance"]intValue];
        totalEarnests = [responseData[@"totalEarnests"]intValue];
        trustee = [responseData[@"trustee"]intValue];
        NSLog(@"balance=%d,totalearnest=%d,trustee=%d",balance,totalEarnests,trustee);
        
    } withFailBlock:^(id errorMsg)
    {
        NSLog(@"%@",errorMsg);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backButtonClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)houseButtonClick:(UIButton *)sender
{
    switch (sender.tag)
    {
        case 1:
        {
            _selectImage_1.hidden = NO;
            _selectImage_2.hidden = YES;
            _selectImage_3.hidden = YES;
        }
            break;
        case 2:
        {
            _selectImage_1.hidden = YES;
            _selectImage_2.hidden = NO;
            _selectImage_3.hidden = YES;
        }
            break;
        case 3:
        {
            _selectImage_1.hidden = YES;
            _selectImage_2.hidden = YES;
            _selectImage_3.hidden = NO;
        }
            break;
        default:
        {
        }
            break;
    }
}
- (IBAction)payButtonClick:(UIButton *)sender
{
    NSLog(@"balance=%d,totalearnest=%d,trustee=%d",balance,totalEarnests,trustee);
    if( balance - totalEarnests - trustee >= 0)
    {
        [HttpServer payEarnestWithMoney:totalEarnests withScheduleId:_scheduleId WithSuccessBlock:^(NSDictionary *responseData)
        {
            [TSMessage showNotificationInViewController:self title:@"支付意向金成功!" subtitle:@"" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
        }
        withFailBlock:^(id errorMsg)
        {
            [TSMessage showNotificationInViewController:self title:@"支付意向金失败!" subtitle:@"" type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
        }];
    }
    else
    {
        PersonAccountViewController *personAccount = [[PersonAccountViewController alloc]init];
        [self.navigationController pushViewController:personAccount animated:YES];
    }
}
@end
